/**********************************************************
 * TranslationsystemAction.java.java
 *
 * Created by punam
 * Last modified Date: Sep 9, 2008 9:00:18 AM by punam
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/
package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.HibernateException;

import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.util.AdminConstants;

/**
 * TranslationsystemAction class list all the translation system, modifies and
 * adds languages.
 *
 * @author punam
 *
 */
public class TranslationsystemAction extends AdminSystemSetup implements
		ServletContextAware {

	private static final long serialVersionUID = 1L;
	private List<String> option = new ArrayList<String>();
	private List<Languages> languagesList = null;
	private Map<String, Languages> myEntitiesMap;
	private Languages languages;
	private String selectedCode = AdminConstants.NONE;
	private static Logger logger = Logger
			.getLogger(TranslationsystemAction.class);

	/**
	 * Displays all the languages for translations systems.
	 *
	 * @return
	 * @throws Exception
	 */
	public String showTranslationSystems() throws Exception {
		logger
				.info("In showshowTranslationSystems method for displying all the languages"
						.intern());
		try {
			displayAllLanguages();
		} catch (HibernateException e) {
			logger.error("Error in showTranslationSystems : ".intern()
					+ e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}

	/**
	 * Updates all the languages in the list, adds new language in the language
	 * table.
	 *
	 * @return
	 */
	public String insertOrUpdate() {
		logger.info("In insertOrUpdate method".intern());
		try {
			for (Languages myEntity : this.getMyEntitiesMap().values()) {
				logger.info("Updating values of all the languages.".intern());
				if (myEntity.getLanguageCode().equals(AdminConstants.NONE)) {
					Languages languageDelete = TranslationSystemServiceDelegator
							.get(myEntity.getOldLanguageCode());
					TranslationSystemServiceDelegator.delete(languageDelete);
				} else {
					myEntity.setDefault_(selectedCode.equals(myEntity
							.getOldLanguageCode()) ? true : false);
					if(myEntity.getActive().equals(false) && myEntity.getDefault_().equals(true)){
						addActionError("Default language shold be active !");
						return INPUT;
					}
					TranslationSystemServiceDelegator.update(myEntity);
				}
			}
			displayAllLanguages();
			if ((languages.getLanguageCode().length() > 1
					&& languages.getLanguageCode().length() < 3)&&(!languages.getLanguageName().equals(AdminConstants.ADD_NEW_LANGUAGE))) {
				logger.info("Adding new language.");
				logger.info(" languageCode :"
						+ languages.getLanguageCode().trim());
				languages
						.setDefault_(selectedCode.equals(AdminConstants.NONE) ? true
								: false);
				TranslationSystemServiceDelegator.add(languages);
			}else{
				if(!languages.getLanguageName().equals(AdminConstants.ADD_NEW_LANGUAGE) && languages.getLanguageCode().equals(AdminConstants.NONE)){
				addActionError("Languagecode is required.");
				return INPUT;
				}
				if(languages.getLanguageCode().length() > 2){
					addActionError("Languagecode should be of two characters.");
					return INPUT;
				}
			}
		} catch (Exception e) {
			logger.error(AdminConstants.ERROR_IN_INSERTING_DATA
					+ e.getMessage());
			addActionError("Enter language code in two characters.");
			return INPUT;
		}finally{
			displayAllLanguages();
		}
		// StrutsFilterDispacher.setTranslatorRule(application);

		addActionMessage("Translation System's configuration successfully saved.".intern());
		return SUCCESS;
	}

	public void displayAllLanguages() {
		this.languagesList = TranslationSystemServiceDelegator.getList();
		this.myEntitiesMap = new HashMap<String, Languages>();
		logger.info("Writing values to map.".intern());
		for (Languages languages : this.getLanguagesList()) {
			this.myEntitiesMap.put(languages.getLanguageCode(), languages);
		}
	}

	public List<String> getOption() {
		return option;
	}

	public void setOption(List<String> option) {
		this.option = option;
	}

	public Languages getTranslationSystem() {
		return languages;
	}

	public void setTranslationSystem(Languages languages) {
		this.languages = languages;
	}

	public List<Languages> getLanguagesList() {
		return languagesList;
	}

	public void setLanguagesList(List<Languages> languagesList) {
		this.languagesList = languagesList;
	}

	public Languages getLanguages() {
		return languages;
	}

	public void setLanguages(Languages languages) {
		this.languages = languages;
	}

	public Map<String, Languages> getMyEntitiesMap() {
		return myEntitiesMap;
	}

	public void setMyEntitiesMap(Map<String, Languages> myEntitiesMap) {
		this.myEntitiesMap = myEntitiesMap;
	}

	public String getSelectedCode() {
		return selectedCode;
	}

	public void setSelectedCode(String selectedCode) {
		this.selectedCode = selectedCode;
	}

	public void setServletContext(ServletContext arg0) {
		application = arg0;

	}

	private ServletContext application;
}
